1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 import java.util.regex.*;
33 import sun.misc.FpUtils;
34 import sun.misc.DoubleConsts;
35
36 public class ParseHexFloatingPoint {
37 private ParseHexFloatingPoint(){}
38
39 public static final double infinityD = Double.POSITIVE_INFINITY;
40 public static final double NaND = Double.NaN;
41
42 static int test(String testName, String input,
43 double result, double expected) {
44 int failures =0;
45
46 if (Double.compare(result, expected) != 0 ) {
47 System.err.println("Failure for " + testName +
48 ": For input " + input +
49 " expected " + expected +
50 " got " + result + ".");
51 }
52
53 return failures;
54 }
55
56 static int testCase(String input, double expected) {
57 int failures =0;
58
59
60
61 input = input.toLowerCase(java.util.Locale.US);
62
63 String [] suffices = {"", "f", "F", "d", "D"};
64 String [] signs = {"", "-", "+"};
65
66 for(int i = 0; i < 2; i++) {
67 String s1 = input;
68 if(i == 1)
69 s1 = s1.replace('x', 'X');
70
71 for(int j = 0; j < 2; j++) {
72 String s2 = s1;
73 if(j == 1)
74 s2 = s2.replace('p', 'P');
75
76 for(int k = 0; k < 2; k++) {
77 String s3 = s2;
78 if(k == 1)
79 s3 = upperCaseHex(s3);
80
81
82 for(int m = 0; m < suffices.length; m++) {
83 String s4 = s3 + suffices[m];
84
85
86 for(int n = 0; n < signs.length; n++) {
87 String s5 = signs[n] + s4;
88
89 double result = Double.parseDouble(s5);
90 failures += test("Double.parseDouble",
91 s5, result, (signs[n].equals("-") ?
92 -expected:
93 expected));
94 }
95 }
96 }
97 }
98 }
99
100 return failures;
101 }
102
103 static String upperCaseHex(String s) {
104 return s.replace('a', 'A').replace('b', 'B').replace('c', 'C').
105 replace('d', 'D').replace('e','E').replace('f', 'F');
106 }
107
108
109
110
111 static int doubleTests() {
112
113
114
115
116 class PairSD {
117 public String s;
118 public double d;
119 PairSD(String s, double d) {
120 this.s = s;
121 this.d = d;
122 }
123 }
124 int failures = 0;
125
126
127
128
129
130
131
132 String leadingZeros = "0x0000000000000000000";
133 String [] threeTests = {
134 "0x.003p12",
135 "0x.006p11",
136 "0x.00cp10",
137 "0x.018p9",
138
139 "0x.3p4",
140 "0x.6p3",
141 "0x.cp2",
142 "0x1.8p1",
143
144 "0x3p0",
145 "0x6.0p-1",
146 "0xc.0p-2",
147 "0x18.0p-3",
148
149 "0x3000000p-24",
150 "0x3.0p0",
151 "0x3.000000p0",
152 };
153 for(int i=0; i < threeTests.length; i++) {
154 String input = threeTests[i];
155 failures += testCase(input, 3.0);
156
157 input.replaceFirst("^0x", leadingZeros);
158 failures += testCase(input, 3.0);
159 }
160
161 long bigExponents [] = {
162 2*DoubleConsts.MAX_EXPONENT,
163 2*DoubleConsts.MIN_EXPONENT,
164
165 (long)Integer.MAX_VALUE-1,
166 (long)Integer.MAX_VALUE,
167 (long)Integer.MAX_VALUE+1,
168
169 (long)Integer.MIN_VALUE-1,
170 (long)Integer.MIN_VALUE,
171 (long)Integer.MIN_VALUE+1,
172
173 Long.MAX_VALUE-1,
174 Long.MAX_VALUE,
175
176 Long.MIN_VALUE+1,
177 Long.MIN_VALUE,
178 };
179
180
181 for(int i = 0; i < bigExponents.length; i++) {
182 failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);
183 }
184
185
186 for(int i = 0; i < bigExponents.length; i++) {
187 long exponent = bigExponents[i];
188 failures += testCase("0x10000.0p"+Long.toString(exponent) ,
189 (exponent <0?0.0:infinityD));
190 }
191
192
193 {
194 long signif = 0;
195 for(int i = 1; i <= 0xe; i++) {
196 signif = (signif <<4) | (long)i;
197 failures += testCase("0x"+Long.toHexString(signif)+"p0", signif);
198 }
199 }
200
201 PairSD [] testCases = {
202 new PairSD("0x0.0p0", 0.0/16.0),
203 new PairSD("0x0.1p0", 1.0/16.0),
204 new PairSD("0x0.2p0", 2.0/16.0),
205 new PairSD("0x0.3p0", 3.0/16.0),
206 new PairSD("0x0.4p0", 4.0/16.0),
207 new PairSD("0x0.5p0", 5.0/16.0),
208 new PairSD("0x0.6p0", 6.0/16.0),
209 new PairSD("0x0.7p0", 7.0/16.0),
210 new PairSD("0x0.8p0", 8.0/16.0),
211 new PairSD("0x0.9p0", 9.0/16.0),
212 new PairSD("0x0.ap0", 10.0/16.0),
213 new PairSD("0x0.bp0", 11.0/16.0),
214 new PairSD("0x0.cp0", 12.0/16.0),
215 new PairSD("0x0.dp0", 13.0/16.0),
216 new PairSD("0x0.ep0", 14.0/16.0),
217 new PairSD("0x0.fp0", 15.0/16.0),
218
219
220
221 new PairSD("0x1.0p-1075", 0.0),
222
223
224
225 new PairSD("0x1.1p-1075", Double.MIN_VALUE),
226 new PairSD("0x1.000000000001p-1075", Double.MIN_VALUE),
227 new PairSD("0x1.000000000000001p-1075", Double.MIN_VALUE),
228
229
230 new PairSD("0x0.fffffffffffff7fffffp-1022", FpUtils.nextDown(DoubleConsts.MIN_NORMAL)),
231 new PairSD("0x0.fffffffffffff8p-1022", DoubleConsts.MIN_NORMAL),
232 new PairSD("0x0.fffffffffffff800000001p-1022",DoubleConsts.MIN_NORMAL),
233 new PairSD("0x0.fffffffffffff80000000000000001p-1022",DoubleConsts.MIN_NORMAL),
234 new PairSD("0x1.0p-1022", DoubleConsts.MIN_NORMAL),
235
236
237
238 new PairSD("0x1.fffffffffffffp1023", Double.MAX_VALUE),
239 new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),
240 new PairSD("0x1.fffffffffffff4p1023", Double.MAX_VALUE),
241 new PairSD("0x1.fffffffffffff7fffffp1023", Double.MAX_VALUE),
242 new PairSD("0x1.fffffffffffff8p1023", infinityD),
243 new PairSD("0x1.fffffffffffff8000001p1023", infinityD),
244
245 new PairSD("0x1.ffffffffffffep1023", FpUtils.nextDown(Double.MAX_VALUE)),
246 new PairSD("0x1.ffffffffffffe0000p1023", FpUtils.nextDown(Double.MAX_VALUE)),
247 new PairSD("0x1.ffffffffffffe8p1023", FpUtils.nextDown(Double.MAX_VALUE)),
248 new PairSD("0x1.ffffffffffffe7p1023", FpUtils.nextDown(Double.MAX_VALUE)),
249 new PairSD("0x1.ffffffffffffeffffffp1023", Double.MAX_VALUE),
250 new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),
251 };
252
253 for (int i = 0; i < testCases.length; i++) {
254 failures += testCase(testCases[i].s,testCases[i].d);
255 }
256
257 failures += significandAlignmentTests();
258
259 {
260 java.util.Random rand = new java.util.Random();
261
262
263 for(int i = 0; i < 1000; i++) {
264 double d = rand.nextDouble();
265 failures += testCase(Double.toHexString(d), d);
266 }
267 }
268
269 return failures;
270 }
271
272
273
274
275
276
277
278 static int significandAlignmentTests() {
279 int failures = 0;
280
281 long [] baseSignifs = {
282 0x1ffffffffffffe00L,
283 0x1fffffffffffff00L
284 };
285
286 double [] answers = {
287 FpUtils.nextDown(FpUtils.nextDown(2.0)),
288 FpUtils.nextDown(2.0),
289 2.0
290 };
291
292 int baseExp = -60;
293 int count = 0;
294 for(int i = 0; i < 2; i++) {
295 for(long j = 0; j <= 0xfL; j++) {
296 for(long k = 0; k <= 8; k+= 4) {
297 long base = baseSignifs[i];
298 long testValue = base | (j<<4) | k;
299
300 int offset = 0;
301
302
303
304 if ((base & 0x100L) == 0L ) {
305 if ( (j >= 8L) &&
306 ((j & 0x7L) != 0 || k != 0 ) )
307 offset = 1;
308 }
309 else {
310 if (j >= 8L)
311 offset = 1;
312 }
313
314 double expected = answers[i+offset];
315
316 for(int m = -2; m <= 3; m++) {
317 count ++;
318
319
320 String s = "0x" +
321 Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) +
322 "p" + (baseExp - m);
323
324 failures += testCase(s, expected);
325 }
326 }
327 }
328 }
329
330 return failures;
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 static int floatTests(){
351 int failures = 0;
352
353
354
355
356 class PairSD {
357 public String s;
358 public float f;
359 PairSD(String s, float f) {
360 this.s = s;
361 this.f = f;
362 }
363 }
364
365 String [][] roundingTestCases = {
366
367
368 {"0x1.000000p0", "0x1.0000000000001p0"},
369
370
371 {"0x1.000002p0", "0x1.0000010000001p0"},
372 {"0x1.000002p0", "0x1.00000100000008p0"},
373 {"0x1.000002p0", "0x1.0000010000000fp0"},
374 {"0x1.000002p0", "0x1.00000100000001p0"},
375 {"0x1.000002p0", "0x1.00000100000000000000000000000000000000001p0"},
376 {"0x1.000002p0", "0x1.0000010000000fp0"},
377
378
379 {"0x1.000002p0", "0x1.000002fffffffp0"},
380 {"0x1.000002p0", "0x1.000002fffffff8p0"},
381 {"0x1.000002p0", "0x1.000002ffffffffp0"},
382
383 {"0x1.000002p0", "0x1.000002ffff0ffp0"},
384 {"0x1.000002p0", "0x1.000002ffff0ff8p0"},
385 {"0x1.000002p0", "0x1.000002ffff0fffp0"},
386
387
388 {"0x1.000000p0", "0x1.000000fffffffp0"},
389 {"0x1.000000p0", "0x1.000000fffffff8p0"},
390 {"0x1.000000p0", "0x1.000000ffffffffp0"},
391
392 {"0x1.000000p0", "0x1.000000ffffffep0"},
393 {"0x1.000000p0", "0x1.000000ffffffe8p0"},
394 {"0x1.000000p0", "0x1.000000ffffffefp0"},
395
396
397 {"0x0.000002p-126", "0x0.0000010000001p-126"},
398 {"0x0.000002p-126", "0x0.00000100000000000001p-126"},
399
400 {"0x0.000006p-126", "0x0.0000050000001p-126"},
401 {"0x0.000006p-126", "0x0.00000500000000000001p-126"},
402
403 {"0x0.0p-149", "0x0.7ffffffffffffffp-149"},
404 {"0x1.0p-148", "0x1.3ffffffffffffffp-148"},
405 {"0x1.cp-147", "0x1.bffffffffffffffp-147"},
406
407 {"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"},
408 };
409
410 String [] signs = {"", "-"};
411
412 for(int i = 0; i < roundingTestCases.length; i++) {
413 for(int j = 0; j < signs.length; j++) {
414 String expectedIn = signs[j]+roundingTestCases[i][0];
415 String resultIn = signs[j]+roundingTestCases[i][1];
416
417 float expected = Float.parseFloat(expectedIn);
418 float result = Float.parseFloat(resultIn);
419
420 if( Float.compare(expected, result) != 0) {
421 failures += 1;
422 System.err.println("" + (i+1));
423 System.err.println("Expected = " + Float.toHexString(expected));
424 System.err.println("Rounded = " + Float.toHexString(result));
425 System.err.println("Double = " + Double.toHexString(Double.parseDouble(resultIn)));
426 System.err.println("Input = " + resultIn);
427 System.err.println("");
428 }
429 }
430 }
431
432 return failures;
433 }
434
435 public static void main(String argv[]) {
436 int failures = 0;
437
438 failures += doubleTests();
439 failures += floatTests();
440
441 if (failures != 0) {
442 throw new RuntimeException("" + failures + " failures while " +
443 "testing hexadecimal floating-point " +
444 "parsing.");
445 }
446 }
447
448 }